home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / testtcpserver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  3.3 KB  |  197 lines  |  [TEXT/MPS ]

  1. #include <Events.h>
  2. #include <memory.h>
  3. #include <types.h>
  4. #include <OSUtils.h> /* for SysBeep */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10. #include <sys/errno.h>
  11. #include <sys/socket.h>
  12. #include <sys/ioctl.h>
  13. #include <netinet/in.h>
  14. #include <sys/uio.h>
  15.  
  16. #include "tcpglue.h"
  17. #include "socket.internal.h"
  18.  
  19. static struct timeval selectPoll = {0,0};
  20. main()
  21. {
  22.     int status;
  23.     int s,s1,s2;
  24.     struct sockaddr_in me, her, name;
  25.     int namelen;
  26.     int herlen = sizeof(her);
  27.     int bytes;
  28.     char line[1000];
  29.     int count,readfds, writefds, exceptfds;
  30.     
  31. #if 1
  32.     dprintf("address %08x\n",xIPAddr());
  33.     dprintf("netmask %08x\n",xNetMask());
  34.     dprintf("max mtu %d\n",xMaxMTU());
  35. #endif
  36.  
  37.     /* make our socket to listen on */
  38.     s = s_socket(AF_INET, SOCK_STREAM, 0);
  39.     if (s < 0)
  40.     {
  41.         perror("socket");    
  42.         exit(1);
  43.     }
  44.     
  45.     /* bind it to port 25 */
  46.     bzero((char *)&me, sizeof(me));
  47.     me.sin_family = AF_INET;
  48.     me.sin_port = htons(25);
  49.     if (s_bind(s, (caddr_t)&me, sizeof(me), 0) < 0)
  50.     {
  51.         perror("bind");    
  52.         exit(1);
  53.     }
  54.     
  55. #if 1
  56.     /* go non-blocking */
  57.     if (s_ioctl(s,FIONBIO,0) < 0)
  58.     {
  59.         perror("ioctl(FIONBIO)");    
  60.         exit(1);
  61.     }
  62. #endif
  63.  
  64.     /* start listening */
  65.     if (s_listen(s,1) < 0)
  66.     {
  67.         perror("listen");    
  68.         exit(1);
  69.     }
  70.     
  71.     /* loop checking for a connection to come in */
  72.     for (;;)
  73.     {
  74.         tcpCheckNotify();
  75.         readfds = 0xffffffff;
  76.         writefds = 0xffffffff;
  77.         count = s_select(32, &readfds, &writefds, (int *)0, &selectPoll);
  78.         if (count < 0)
  79.         {
  80.             perror("select");
  81.             exit(1);
  82.         }
  83.         if (count > 0)
  84.             break;
  85.     }
  86.     dprintf("count %d readfds %08x writefds %08x\n",count,readfds,writefds);
  87.     
  88.     /* accept the connection */
  89.     s1 = s_accept(s,&her,&herlen);
  90.     if (s1 < 0)
  91.     {
  92.         perror("accept");
  93.         exit(1);
  94.     }
  95.     dprintf("talking to %08x/%d on %d\n",her.sin_addr,her.sin_port,s1);
  96.     
  97.     /* close the listen socket */
  98.     status = s_close(s);
  99.     if (status < 0)
  100.     {
  101.         perror("close(s)");
  102.         exit(1);
  103.     }
  104.  
  105.     /* send out a greeting */
  106.     tcpCheckNotify();
  107.     strcpy(line,"hello from the test server\015\012");
  108.     bytes = s_write(s1,line,strlen(line));
  109.     if (bytes < 0 && errno != EINPROGRESS)
  110.     {
  111.         perror("write");
  112.         exit(1);
  113.     }
  114.  
  115.     /* wait for the write to finish */    
  116.     for (;;)
  117.     {
  118.         tcpCheckNotify();
  119.         writefds = 0xffffffff;
  120.         count = s_select(32, NULL, &writefds, NULL, &selectPoll);
  121.         if (count < 0)
  122.         {
  123.             perror("select");
  124.             exit(1);
  125.         }
  126.         if (count > 0)
  127.             break;
  128.     }
  129.  
  130.     /* wait for an answer */
  131.     for (;;)
  132.     {
  133.         tcpCheckNotify();
  134.         readfds = 0xffffffff;
  135.         count = s_select(32, &readfds, (int *)0, (int *)0, &selectPoll);
  136.         if (count < 0)
  137.         {
  138.             perror("select");
  139.             exit(1);
  140.         }
  141.         if (count > 0)
  142.             break;
  143.     }
  144.  
  145.     /* read the answer */
  146.     tcpCheckNotify();
  147.     bytes = s_read(s1,line,sizeof(line)-2);
  148.     if (bytes < 0)
  149.     {
  150.         perror("read");
  151.         exit(1);
  152.     }
  153.     dprintf("read got %d bytes\n",bytes);
  154.     line[bytes] = '\0';
  155.     dprintf("'%s'\n",line);
  156.     
  157.     /* send a goodbye message */
  158.     tcpCheckNotify();
  159.     strcpy(line,"over and out\015\012");
  160.     bytes = s_write(s1,line,strlen(line));
  161.     if (bytes < 0 && errno != EINPROGRESS)
  162.     {
  163.         perror("write");
  164.         exit(1);
  165.     }
  166.  
  167.     /* wait for it to get there */    
  168.     for (;;)
  169.     {
  170.         tcpCheckNotify();
  171.         writefds = 0xffffffff;
  172.         count = s_select(32, NULL, &writefds, NULL, &selectPoll);
  173.         if (count < 0)
  174.         {
  175.             perror("select");
  176.             exit(1);
  177.         }
  178.         if (count > 0)
  179.             break;
  180.     }
  181.  
  182.     /* clean up */
  183.     status = s_close(s1);
  184.     if (status < 0)
  185.     {
  186.         perror("close(s1)");
  187.         exit(1);
  188.     }
  189.  
  190.     /* spin incase something odd happens */
  191.     for (;;)
  192.     {
  193.         tcpCheckNotify();
  194.     }
  195. }
  196.  
  197.